/**
 * 转换接口注释为单行
 * @param interfaceCode
 * @returns
 */
export function convertInterfaceCommentsToSingleLine(interfaceCode: string = '') {
  // 正则表达式匹配多行注释 /** ... */
  const regex = /\/\*\*([\s\S]*?)\*\//g;

  // 替换每个匹配到的注释块
  return interfaceCode.replace(regex, (match, content) => {
    // 去掉每行的前导星号和空格，并合并为单行
    const singleLineContent = content
      .split('\n') // 按行分割
      ?.map((line:string) => line.replace(/^\s*\*?\s?/, '').trim()) // 去掉每行的 * 和空格
      .filter((line:string) => line.length > 0) // 过滤掉空行
      .join(' '); // 合并成一行

    // 返回单行注释
    return `/** ${singleLineContent} */`;
  });
}

/**
 * 首字母大写
 * @param str
 * @returns
 */
export function capitalizeFirstLetter(str:string) {
  if (!str) return str; // 处理空字符串
  return str.charAt(0).toUpperCase() + str.slice(1);
}


/**
 * 生成 api 函数
 * @param data
 * @returns
 */
export function genApiFunction(data:any) {
  const { path, title } = data ?? {};

  if (!path) return '';
  const pathArr = path.split('/');
  const pathArrReverse = pathArr.reverse();
  const fnName = [ pathArrReverse[1], capitalizeFirstLetter(pathArrReverse?.[0]) ].join('');
  const reqInterfaceName = capitalizeFirstLetter(fnName) + 'Req';
  const resInterfaceName = capitalizeFirstLetter(fnName) + 'Resp';

  return `
/** ${title} POST ${path} */
export function ${fnName}(params: NAMESPACE.${reqInterfaceName}) {
    return request.post<NAMESPACE.${resInterfaceName}>(
        "${path}",
        params
    );
}`;
}
